package com.yswee.framework.widget.layoutview;

import com.yswee.framework.activity.IActivityContext;
import com.yswee.framework.manager.ActivityManager;
import com.yswee.framework.utils.ViewInjectUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;

public abstract class MFrameLayout<T> extends android.widget.FrameLayout implements ILayoutView<T> {

	protected T mDataItem;
	protected Context mContext;
	protected int mPosition = 0;
	protected int mTotalCount = 0;
	
	public MFrameLayout(Context context) {
		super(context);
		initializeLayout(context);
	}
	
	public MFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initializeLayout(context);
	}
	
	public MFrameLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initializeLayout(context);
	}
	
	protected void initializeLayout(Context context) {
		mContext = context;
		setContentView();
		onInjectView();
		onFindView();
		onBindListener();
	}
	
	/**
	 * 设置ContentView
	 */
	protected void setContentView() {
		int resId = getLayoutResId();
		if(resId != 0) {
			LayoutInflater.from(mContext).inflate(resId, this, true);
		}
	}
	
	/**
	 * 设置view在listview的位置
	 * @param position
	 */
	public void setPosition(int totalcount, int position) {
		mPosition = position;
		mTotalCount = totalcount;
	}
	
	@Override
	public int getPosition() {
		return mPosition;
	}
	
	@Override
	public boolean isFirst() {
		return mTotalCount > 0 && mPosition == 0;
	}
	
	@Override
	public boolean isLast() {
		return mTotalCount > 0 && mPosition == mTotalCount - 1;
	}
	
	/**
	 * 设置数据源进行渲染
	 * @param t
	 */
	public void setDataSource(T t) {
		mDataItem = t;
		onApplyData();
		onBindListener();
	}
	
	/**
	 * 刷新数据
	 */
	public void notifyDataSetChanged() {
		onApplyData();
	}
	
	/**
	 * 取得数据源
	 * @return
	 */
	public T getDataSource() {
		return mDataItem;
	}

	/**
	 * 获取布局文件resid
	 * @return
	 */
	protected int getLayoutResId() {
		return 0;
	}
	
	/**
	 * 初始化控件、获取内部控件（注释类遍历）
	 */
	protected void onInjectView() {
		ViewInjectUtils.onInjectView(this);
	}
	
	/**
	 * 初始化控件、获取内部控件
	 */
	protected void onFindView() {
		
	}
	
	/**
	 * 设置事件
	 */
	protected void onBindListener() {
		
	}
	
	/**
	 * 渲染控件数据
	 */
	protected abstract void onApplyData();
	
	protected void runOnUiThread(Runnable action) {
		Activity activity = null;
		if (mContext instanceof Activity) {
			activity = (Activity) mContext;
		} else {
			IActivityContext context = ActivityManager.get().currentActivity();
			if (context != null) {
				activity = (Activity) context;
			}
		}
		if (activity != null) {
			activity.runOnUiThread(action);
		}
	}
	
	protected void startActivity(Class<?> cls) {
		Intent intent = new Intent(mContext, cls);
		startActivity(intent);
	}
	
	protected void startActivityForResult(Class<?> cls, int requestCode) {
		Intent intent = new Intent(mContext, cls);
		startActivityForResult(intent, requestCode);
	}
	
	public void startActivity(Intent intent) {
		mContext.startActivity(intent);
    }
	
	public void startActivityForResult(Intent intent, int requestCode) {
		Activity activity = (Activity) mContext;
		activity.startActivityForResult(intent, requestCode);
    }

}
